from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from apps.accounts.serializers import UserSerializer
from apps.master.models import SerialNumberModel, DepartmentsModel, CurrencyRateModel, CurrencyModel
from bases.apifields import ApiDecimalField
from bases.serializers import APIModelSerializer


class SerialNumberSerializer(APIModelSerializer):
    serial_number = serializers.CharField(label='制番号', max_length=12, allow_blank=True, allow_null=True, required=False)
    serial_name = serializers.CharField(label='制番名称', max_length=50, allow_blank=True, allow_null=True, required=False)

    class Meta:
        model = SerialNumberModel
        fields = '__all__'

    def validate(self, attrs):
        serial_number = attrs.get('serial_number')

        if self.request.method.lower() == 'post':
            if SerialNumberModel.objects.filter(serial_number__exact=serial_number).exists():
                # Translators: 在系统中已经存在
                self.handle_validation('serial_number',"在系统中已经存在")
        else:
            if SerialNumberModel.objects.exclude(pk=self.instance.pk).filter(serial_number__exact=serial_number).exists():
                self.handle_validation('serial_number',"在系统中已经存在")

        return super().validate(attrs)

class DepartmentsSerializer(APIModelSerializer):
    code = serializers.CharField(label='部门代码', max_length=3, allow_blank=True, allow_null=True, required=False)
    name = serializers.CharField(label='部门名称', max_length=10, allow_blank=True, allow_null=True, required=False)
    departments = UserSerializer(many=True, read_only=True)

    class Meta:
        model = DepartmentsModel
        fields = '__all__'

    def validate(self, attrs):
        code = attrs.get('code')

        if self.request.method.lower() == 'post':
            if DepartmentsModel.objects.filter(code__exact=code).exists():
                # Translators: 在系统中已经存在
                self.handle_validation('code',"在系统中已经存在")
        else:
            if DepartmentsModel.objects.exclude(pk=self.instance.pk).filter(code__exact=code).exists():
                self.handle_validation('code',"在系统中已经存在")

        return super().validate(attrs)


class CurrencySerializer(APIModelSerializer):
    currency = serializers.CharField(label='币种', max_length=3, allow_blank=True, allow_null=True, required=False)

    class Meta:
        model = CurrencyModel
        fields = '__all__'

    def validate(self, attrs):
        currency = attrs.get('currency')
        if self.request.method.lower() == 'post':
            if CurrencyModel.objects.filter(currency__exact=currency).exists():
                self.handle_validation('currency', '币种在系统中已经存在')
        else:
            if CurrencyModel.objects.exclude(pk=self.instance.pk).filter(currency__exact=currency).exists():
                self.handle_validation('currency', '币种在系统中已经存在')

        return super().validate(attrs)


class CurrencyRateSerializer(APIModelSerializer):
    standard_curr = serializers.CharField(label='标准币种', max_length=3, allow_blank=True, allow_null=True, required=False)
    quote_curr = serializers.CharField(label='报价币种', max_length=3, allow_blank=True, allow_null=True, required=False)
    rate_date = serializers.DateField(label='汇率日期', allow_null=True, required=False)
    exchange_rate = ApiDecimalField(label='汇率', allow_null=True, decimal_places=2, max_digits=12, required=False)

    class Meta:
        model = CurrencyRateModel
        fields = '__all__'

    def validate(self, attrs):
        standard_curr = attrs.get('standard_curr')
        quote_curr = attrs.get('quote_curr')
        rate_date = attrs.get('rate_date')
        exchange_rate = attrs.get('exchange_rate')

        if exchange_rate and exchange_rate <= 0:
            self.handle_validation('exchange_rate', '不允许小于等于0')

        if self.request.method.lower() == 'post':
            if CurrencyRateModel.objects.filter(standard_curr__exact=standard_curr, quote_curr__exact=quote_curr,
                                        rate_date__exact=rate_date).exists():
                raise ValidationError("标准币种+报价币种+报价日期的组合在系统中已经存在")
        else:
            if CurrencyRateModel.objects.exclude(pk=self.instance.pk).filter(standard_curr__exact=standard_curr, quote_curr__exact=quote_curr,
                                        rate_date__exact=rate_date).exists():
                raise ValidationError("标准币种+报价币种+报价日期的组合在系统中已经存在")

        return super().validate(attrs)